<?php

namespace app\common\library;

use fast\Http;
use think\Db;
use think\exception\ValidateException;
use think\Hook;

/**
 * 邮箱验证码类
 */
class EvaluationManager
{

    protected $appid = '20190827164752';  //appid
    protected $sercret = 'M9VfOrB4rRFw6v7cLcbyOukALT6HOPgV'; //密钥
    protected $options = [];
    protected static $ADD_CHILDREN = 'https://api.jingyunzhixin.com/api/children';
    protected static $ADD_USERS = 'https://api.jingyunzhixin.com/api/users';
    protected static $CREATE_TEST = 'https://api.jingyunzhixin.com/api/assesses';
    protected static $TEST_detail = 'https://api.jingyunzhixin.com/api/assesses';

    public function __construct()
    {
//        $config = Db::name('evaluation_config')->field('*')->order('id', 'desc')->limit(1)->find();
        $config = $config = config('config')['evaluation'];;
        $this->appid = isset($config['appid'])?$config['appid']:'20190827164752';
        $this->sercret = isset($config['secret'])?$config['secret']:'M9VfOrB4rRFw6v7cLcbyOukALT6HOPgV';
        $this->options = [
            'accept: application/vnd.jyzx.agent-v1+json',
            'appid:'.$this->appid,
            'secret:'.$this->sercret
        ];
        $scalar_ids = [

        ];
    }

    /**
        添加测评人员
     */
    public function addChildren($data) {
        $defaults[CURLOPT_HTTPHEADER] = $this->options;
        $res = Http::post(self::$ADD_CHILDREN, $data, $defaults);
        $res_decode = json_decode($res, true);

        return $res_decode;
    }

    /**
    添加测评人员
     */
    public function addUsers($data) {
        $defaults[CURLOPT_HTTPHEADER] = $this->options;
        $res = Http::post(self::$ADD_USERS, $data, $defaults);
        $res_decode = json_decode($res, true);

        return $res_decode;
    }
    /**
        创建测评
     */
    public function create($order, $is_parent) {
        $defaults[CURLOPT_HTTPHEADER] = $this->options;
        if($is_parent == 1){
            $data['openid'] = $order['openid'];
        }else{
            $data['user_id'] = $order['openid'];
        }
        $data['scalar_id'] = $order['scalar_id'];
        $data['no'] = $order['order_sn'];
        $res = Http::post(self::$CREATE_TEST, $data, $defaults);
        $res_decode = json_decode($res, true);
        return $res_decode;
//        if (isset($res_decode['status']) && $res_decode['status'] == 0) {
//            return $res_decode['number'];
//        } else {
//            throw new ValidateException($res_decode['message']);
//        }

    }


    /**
        测评详情
     */
    public function detail($number) {
        $defaults[CURLOPT_HTTPHEADER] = $this->options;
        $url = self::$CREATE_TEST . '/' . $number;
        $res = Http::get($url, [], $defaults);
        $res_decode = json_decode($res, true);
        if (isset($res_decode[0]) && isset($res_decode[0]['status'])) {
            $res = $res_decode[0];
            if ($res['status'] == 5) {
                $res['url'] = 'https://jigou.jingyunzhixin.com/report/' . $res['link']. '/index.html?number='.$res['number'].'&ismini=1';
//                $res['url'] = 'https://jigou.jingyunzhixin.com/report/' . $res['link']. '/index.html';
            } else {
                $res['url'] = 'https://jigou.jingyunzhixin.com/question1/#/question?number=' . $res['number'];
//                $res['url'] = 'https://jigou.jingyunzhixin.com/question1/#/question' ;
            }

            return $res;
        } else {
            return false;
//            throw new ValidateException('获取连接失败');
        }
    }

}
